home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / glue / checkglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-31  |  5.7 KB  |  191 lines

  1. /*
  2.  
  3.     This file is a part of the GLASS source distribution 
  4.     and therefore subjected to the copy notice below. 
  5.     
  6.     Copyright (C) 1989,1990  S.J. Klaver, R Doesborg
  7.               email: simon@sagan.nl
  8.  
  9.     This program is free software; you can redistribute it and/or modify
  10.     it under the terms of the GNU General Public License as published by
  11.     the Free Software Foundation version 1
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*-----------------------------------------------------------------------
  23.    File: checkglue.c
  24.    Date: 18-10-1989
  25.    Author: R. Doesborg
  26.    Contents: procedures which check the consistency of the description
  27.              of menus and parameter menus. The following checks
  28.              are performed:
  29.              - all types of default values should be correct;
  30.              - if a menu refers to menu 'n', then menu 'n' should
  31.                exist;
  32.              - 
  33.      Furthermore:
  34.              - a procedure which returns a pointer to a menu, given the id
  35.                of that menu;
  36.              - a procedure which adds the item "Exit" to the main menu,
  37.                unless this item was already specified.
  38. -------------------------------------------------------------------------*/
  39.  
  40. #include <stdio.h>
  41. #include <tmc.h>
  42. #include "menuds.h"
  43. #include "tools.h"
  44. #include "globalvars.h"
  45.  
  46. extern char *malloc();
  47. extern char *strcpy();
  48. extern char *strcat();
  49.  
  50. /*
  51.     add_exit adds the item "Exit" to the main menu, provided this item does
  52.     not yet exist. It may fail to do so if there already exists an item
  53.     in the main menu which has 'x' as identifying character and is not
  54.     "Exit" (for example "Editor").
  55. */
  56. int add_exit (gl)
  57.   gluefile gl;
  58. {
  59.   Menu mainmenu;
  60.   menuitemspec newitem;
  61.   Do act;
  62.   int it;
  63.   int err = 0;
  64.   actionspec_list newlist;
  65.  
  66.   mainmenu = find_menu(gl, mainmenuid);
  67.   it = find_item(mainmenu, 'x');
  68.   if (it == -1) {
  69.     act = (Do)malloc(sizeof(*act));
  70.     act->tag = TAGDo;
  71.     act->actionno = -1;
  72.     newlist = new_actionspec_list();
  73.     newitem = new_menuitemspec("Exit", 0, 'x', 2, newlist);
  74.     app_actionspec_list(newitem->action, (actionspec)act);
  75.     app_menuitemspec_list(mainmenu->menuitems, newitem);
  76.   }
  77.   else
  78.     err = (strcmp(mainmenu->menuitems->arr[it]->itemname, "Exit") != 0);
  79.   return(err);
  80. }
  81.  
  82. /* type_correct checks the type of a string:
  83.    Boolean ==> str = "TRUE" or str = "FALSE";
  84.    Integer ==> str = [-](0123456789)*;
  85.    Real    ==> str = [-](0123456789)*[.(0123456789)*][E[-](0123456789)*];
  86.    Enum    ==> str in strlist (which is found in the type specification);
  87.    String  ==> true
  88. */
  89. int type_correct (str, typ)
  90.   string str;
  91.   typespec typ;
  92. {
  93.   int correct;
  94.   int ind;
  95.   string_list slist;
  96.   char *hstr;
  97.  
  98.   hstr = converttouppercase(str);
  99.   switch (typ->tag) {
  100.     case TAGBoolean:
  101.       correct = (strcmp(hstr, "TRUE") == 0 || strcmp(hstr, "FALSE") == 0);
  102.       break;
  103.     case TAGInteger:
  104.       if (*hstr == '-') hstr++;
  105.       correct = 1;
  106.       while (correct && *hstr != '\0') {
  107.         correct = (*hstr >= '0' && *hstr <= '9');
  108.         hstr++;
  109.       }
  110.       break;
  111.     case TAGString:
  112.       correct = 1;
  113.       break;
  114.     case TAGReal:
  115.       if (*hstr == '-') hstr++;
  116.       while (*hstr == ' ') hstr++;
  117.       correct = (*hstr>='0' && *hstr <= '9');
  118.       while (*hstr>='0' && *hstr <= '9') hstr++;
  119.       if (*hstr == '.') {
  120.         hstr++;
  121.         while (*hstr>='0' && *hstr<='9') hstr++;
  122.       }
  123.       if (*hstr == 'E') {
  124.         hstr++;
  125.         if (*hstr == '-') hstr++;
  126.         while (*hstr >= '0' && *hstr <= '9') hstr++;
  127.       }
  128.       correct = correct && (*hstr == '\0');
  129.       break;
  130.     case TAGEnum:
  131.       slist = ((Enum)typ)->enumvals;
  132.       for (ind = 0, correct = 0; !correct && ind < slist->sz; ind++)
  133.         correct = (strcmp(hstr, slist->arr[ind]) == 0);
  134.       break;
  135.   }
  136.   return(correct);
  137. }
  138.  
  139. /* Performs some checks on the data structure: it checks whether
  140.    references to other menus really exist, and it checks the type
  141.    correctness of the default values in parameter sets.
  142.    Furthermore, it adds the item 'EXIT' to the main menu.
  143.    On errors, the function returns 1, 0 otherwise.
  144.  
  145.    If mainparid != 0 there are no menus in the data structure.
  146. */
  147. int check_glueds (gl, mainparid)
  148.   gluefile gl;
  149.   int mainparid;
  150. {
  151.   menparspec_list mpsp;
  152.   menuitemspec_list mlist;
  153.   paritemspec_list plist;
  154.   int i, j, id;
  155.   Menu mn;
  156.   int err = 0;
  157.   int loc_err;
  158. /*
  159.   mpsp = ((Glue)gl) -> specs;
  160.   for (i=0; i<mpsp->sz; i++)
  161.     if ((mpsp->arr[i])->tag == TAGMenu) {
  162.       mlist = ((Menu)mpsp->arr[i])->menuitems;
  163.       for (j=0; j<mlist->sz; j++) {
  164.         if ((mlist->arr[j])->tag == TAGShow) {
  165.           id = ((Show)mlist->arr[j]->action)->menuid;
  166.           mn = find_menu(gl, id);
  167.           if (mn == NULL)
  168.             printf("\nGLUE-E, Menu %d used, but not defined", id);
  169.           err = err || (mn == NULL);
  170.         }
  171.       }
  172.     }
  173.     else {
  174.       plist = ((Parset)mpsp->arr[i])->parsetitem;
  175.       for (j=0; j<plist->sz; j++) {
  176.         if (plist->arr[j]->partype->tag == TAGEnum)
  177.           convertlisttouppercase(((Enum)plist->arr[j]->partype)->enumvals);
  178.         loc_err = !type_correct(plist->arr[j]->pardefault,
  179.                                 plist->arr[j]->partype);
  180.         if (loc_err)
  181.           printf("GLUE-E, Default %s is not of the specified type\n",
  182.                        plist->arr[j]->pardefault);
  183.         err = err || loc_err;
  184.       }
  185.     }
  186. */
  187.   if (mainparid == 0) err = err || add_exit(gl);
  188.   return(err);
  189. }
  190.  
  191.